﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input.ManageInput;
using IntelligentAgriculture.Contracts.Dto.Output.ManageOutput;
using IntelligentAgriculture.Contracts.Dto.Output.PageOutput;
using IntelligentAgriculture.Contracts.Interface.IManagements;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Impl.Managements;
using IntelligentAgriculture.Domain.Interface.IManagement;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services.Management
{
    public class ManagService : IManageService
    {
        private readonly IManageRepository _Managerepository;
        private readonly ILogger<ManagService> _logger;
        
        private readonly IMapper _mapper;
        public ManagService(IManageRepository managerepository, ILogger<ManagService> logger, IMapper mapper)
        {
            _Managerepository = managerepository;
            _logger = logger;
            _mapper = mapper;
        }


        /// <summary>
        /// 添加功能
        /// </summary>
        /// <param name="entity">方法参数</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Add(ManagerAddInput entity)
        {
            try
            {
                var Addmanager = _mapper.Map<Domain.Entity.Management>(entity);
                return await _Managerepository.Add(Addmanager);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="entity">修改实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int>Update(ManagerAddInput entity)
        {
            try
            {
                var updatedManager = _mapper.Map<Domain.Entity.Management>(entity);
                return await _Managerepository.Update(updatedManager); 
            }
            catch (Exception)
            {

                throw;
            }   
        }
       
       

      
        /// <summary>
        /// 显示，查询，分页功能
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>列表</returns>
        public PagesOutput<ManageShowOutput> GetShow(ManageShowinput input)
        {
            var list = _Managerepository.GetShow().AsNoTracking();
            if (!string.IsNullOrEmpty(input.Corresponding))
            {
                list = list.Where(x => x.Corresponding.Contains(input.Corresponding));
            }
            if (!string.IsNullOrEmpty(input.EarTag))
            {
                list = list.Where(x => x.EarTag.Contains(input.EarTag));
            }
            if (!string.IsNullOrEmpty(input.BirthStatus))
            {
                list = list.Where(x => x.BirthStatus.Contains(input.BirthStatus));
            }
            if (input.UseState != null)
            {
                list = list.Where(x => x.UseState == input.UseState);
            }


            var result = new PagesOutput<ManageShowOutput>()
            {
                Total = list.Count(),
                PageSize = input.PageSize
            };

            var query = list.OrderBy(x => x.EarTagId).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
            result.Data = _mapper.Map<List<ManageShowOutput>>(query);
            return result;
        }
        /// <summary>
        /// 根据指定的ID删除单个耳标信息
        /// </summary>
        /// <param name="id">要删除的耳标信息的ID</param>
        /// <returns>返回一个表示操作成功的异步任务，返回删除的记录数</returns>
        public async Task<int> Delete(int id)
        {
            return await _Managerepository.Delete(id);
        }
        /// <summary>
        /// 批量删除指定IDs的耳标信息
        /// </summary>
        /// <param name="ids">要删除的耳标信息的ID集合。</param>
        /// <returns>返回一个表示操作成功的异步任务，返回删除的记录数。</returns>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            return await _Managerepository.DeleteRange(ids);
        }
    }
}

